<template>
  <div class="login_container">
    <div class="login_box">
      <div class="avatr_box">
        <img src="../assets/logo.png" />
      </div>
      <el-form ref="loginform" :model="login" :rules="loginrules" label-width="0px" class="myform">
        <el-form-item prop="name">
          <el-input prefix-icon="iconfont icon-user" v-model="login.name"></el-input>
        </el-form-item>
        <el-form-item prop="pwd">
          <el-input prefix-icon="iconfont icon-lock_fill" v-model="login.pwd" type="password"></el-input>
        </el-form-item>
        <el-form-item class="btns">
          <el-button @click="submitForm('loginform')" type="primary">登录</el-button>
          <el-button @click="resetForm()" type="info">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      login: {
        name: 'admin',
        pwd: '123456'
      },
      //这是表单的验证规则
      loginrules: {
        //验证用户名是否合法
        name: [
          { required: true, message: '请输入姓名', trigger: 'change' },
          { min: 2, max: 5, message: '长度在 2 到 5 个字符', trigger: 'blur' }
        ],
        pwd: [
          { required: true, message: '请输入密码', trigger: 'change' },
          { min: 6, max: 10, message: '长度在 6 到 10 个字符', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    submitForm(formName) {
      this.$refs[formName].validate(async valid => {
        if (valid) {
          //对返回的数据结构重塑一下{data：res}通过res直接获得需要的数据
          const { data: res } = await this.$http.post('login', this.login)
          // console.log(res);
          if (res.meta.status !== 200) return this.$message.error('账号或密码错误')
          this.$message('登录成功')
          //1.姜登录成功的token保存至sessionstorage里面
          //1.1项目中的除了等外之外的接口都需要token密钥才可以进行访问
          //1.2token只在当前网站打开期间生效，所以token要保存再sessionstroge里面
          //2.通过编程时导航跳转到后台主页，路由地址是/home
          //console.log(res.data.token);
          sessionStorage.setItem('token', res.data.token)
          this.$router.push('/home')
        } else {
          console.log('error submit!!')
          return false
        }
      })
    },
    resetForm() {
      sessionStorage.setItem('token', '123456')
      //console.log(this)
      this.$refs.loginform.resetFields()

      this.$router.push('/home')
    }
  }
}
</script>

<style scoped>
.login_container {
  background-color: #2b4b6b;
  height: 100%;
  color: white;
}
.login_box {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  width: 450px;
  height: 300px;
  background-color: white;
  border-radius: 3px;
}
.avatr_box {
  position: absolute;
  left: 50%;
  transform: translate(-50%, -50%);
  height: 130px;
  width: 130px;
  border-radius: 50%;
  border: 1px solid #cccccc;
  padding: 10px;
  box-shadow: 0 0 10px #dddddd;
}
.avatr_box img {
  height: 100%;
  width: 100%;
  border-radius: 50%;
  background-color: #eeeeee;
}
.btns {
  display: flex;
  justify-content: flex-end;
}
.myform {
  position: absolute;
  bottom: 0;
  width: 100%;
  padding: 0px 20px;
  box-sizing: border-box;
}
</style>
